package com.kirayous.blog.controller;


import com.kirayous.api.blog.service.IArticleService;
import com.kirayous.common.Result;
import com.kirayous.common.annotation.OptLog;
import com.kirayous.common.annotation.PagePathPatternsAno;
import com.kirayous.common.constant.OptTypeConst;
import com.kirayous.common.vo.ArticleTopVO;
import com.kirayous.common.vo.ConditionVO;
import com.kirayous.common.vo.DeleteVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author KiraYous
 * @since 2021-09-23
 */
@Api(tags = "文章模块")
@RestController
public class ArticleController {


    @Autowired
    IArticleService articleService;
    /**
     * 查看后台文章
     *
     * @param conditionVO 条件
     * @return 后台文章列表
     *
     * 【后台】
     */
    @ApiOperation(value = "查看后台文章")
    @GetMapping("/admin/articles")
    @PagePathPatternsAno(path = "/admin/articles")
    public Result listArticleBacks(ConditionVO conditionVO) {
        return Result.success(articleService.listArticleBacks(conditionVO));
    }

    /**
     * 根据id查看后台文章
     *
     * @param articleId 文章id
     * @return 后台文章
     * 【后台】
     */
    @ApiOperation(value = "根据id查看后台文章")
    @ApiImplicitParam(name = "articleId", value = "文章id", required = true, dataType = "Integer")
    @GetMapping("/admin/articles/{articleId}")
    public Result getArticleBackById(@PathVariable("articleId") Integer articleId) {
        return Result.success(articleService.getArticleBackById(articleId));
    }


    /**
     * 修改文章置顶状态
     *
     * @param articleTopVO 文章置顶信息
     * @return {@link Result<>}
     * 【后台】
     */
    @OptLog(optType = OptTypeConst.UPDATE)
    @ApiOperation(value = "修改文章置顶")
    @PutMapping("/admin/articles/top")
    public Result  updateArticleTop(@Valid @RequestBody ArticleTopVO articleTopVO) {
        articleService.updateArticleTop(articleTopVO);
        return Result.success();
    }

    /**
     * 恢复或删除文章
     *
     * @param deleteVO 逻辑删除信息
     * @return {@link Result<>}
     * 由于是软删除，不适用Delete ，而是使用put进行状态值的修改
     * 【后台】
     */
    @OptLog(optType = OptTypeConst.UPDATE)
    @ApiOperation(value = "恢复或删除文章")
    @PutMapping("/admin/articles")
    public Result updateArticleDelete(@Valid @RequestBody DeleteVO deleteVO) {
        articleService.updateArticleDelete(deleteVO);
        return Result.success();
    }

    /**
     * 删除文章
     *
     * @param articleIdList 文章id列表
     * @return {@link Result<>}
     *
     * 【后台】
     */
    @OptLog(optType = OptTypeConst.REMOVE)
    @ApiOperation(value = "物理删除文章")
    @DeleteMapping("/admin/articles")
    public Result deleteArticles(@RequestBody List<Integer> articleIdList) {
        articleService.deleteArticles(articleIdList);
        return Result.success();
    }

}

